<?php
	session_start();	
?>

<?php
	if( $_SESSION["admin"] != "" )
	{
?>
<a href="index.php?com=page"><b>List page</b></a> | <a href="index.php?com=page&func=add"><b>Add page</b></a>
<?php
	include_once("process/page.php");	
	$page = new page();
	
	if( $_GET["com"] == "page" && $_GET["func"] == "update" && isset($_GET["id"]))
	{
		$row = $page->GetOnePage($_GET['id']);	
	}elseif( isset($_POST['btnUpdate']) )
	{	
        $file = $_FILES["Picture"];
		$file_name = $file["name"];
		$file_ext = explode(".",$file_name);
		if($file["name"] == "")
		{	
			$DepartmentID = stripcslashes($_POST["DepartmentID"]);
    		$Title=stripcslashes($_POST["Title"]);
    		$Link=stripcslashes($_POST["Link"]);
            $Picture=$file_name;
            $Description=stripcslashes($_POST["Description"]);
            $FeaturedYN=stripcslashes($_POST["FeaturedYN"]);
            $SortPriority=stripcslashes($_POST["SortPriority"]);
            $ActiveYN=stripcslashes($_POST["ActiveYN"]);
              $id = stripcslashes($_POST["PageID"]);
        //echo $code, $name, $order;
            if($Title != "")
            {
        		$page->UpdatePage($DepartmentID, $Title, $Link, $Picture, $Description, $FeaturedYN, $SortPriority, $ActiveYN, $id);	
        	//	echo '<meta http-equiv="Refresh" content="0;URL=index.php?com=page" />';
        		//return;
            }
        }
        if($file_ext[count($file_ext)-1] == "jpg" || $file_ext[count($file_ext)-1] == "jpeg" || $file_ext[count($file_ext)-1] == "gif")
		{
		      $DepartmentID = stripcslashes($_POST["DepartmentID"]);
    		$Title=stripcslashes($_POST["Title"]);
    		$Link=stripcslashes($_POST["Link"]);
            $Picture=$file_name;
            $Description=stripcslashes($_POST["Description"]);
            $FeaturedYN=stripcslashes($_POST["FeaturedYN"]);
            $SortPriority=stripcslashes($_POST["SortPriority"]);
            $ActiveYN=stripcslashes($_POST["ActiveYN"]);
              $id = stripcslashes($_POST["PageID"]);
        //echo $code, $name, $order;
            if($Title != "")
            {
        		$page->UpdatePage($DepartmentID, $Title, $Link, $Picture, $Description, $FeaturedYN, $SortPriority, $ActiveYN, $id);
                move_uploaded_file($_FILES["Picture"]["tmp_name"],"asset/".$_FILES["Picture"]["name"]);	
        	//	echo '<meta http-equiv="Refresh" content="0;URL=index.php?com=page" />';
        		//return;
            }
		}
        else
		{
			$alert='<script type="text/javascript">
			alert("Bạn chỉ được phép chọn file có định dạng jpg, jpeg, gif");
			</script>';
			echo $alert;
		}
	}
	else
	{
		echo '<meta http-equiv="Refresh" content="0;URL=index.php?com=page" />';
	}	
	
?>
<form action="index.php?com=page&func=update" method="post" id="contactform" enctype="multipart/form-data">
	<table width="630px" style="font-weight:bold; font-size:10pt;" border="0">
		<tr>
			<td align="center" colspan="4" height="50"><b style="font-size:12pt; color:Blue;">Update page</b></td>
		</tr>
		<tr height="26">
			<td align="right" width="120px">Title :&nbsp;&nbsp;
				<input type="hidden" name="PageID" id="PageID" value="<?php echo $row['PageID'] ?>" />
			</td>
			<td width="140px"><input type="text" class="validate[required]" name="DepartmentID" id="DepartmentID" value="<?php echo $row['DepartmentID']; ?>" style="width: 120px;" /><font style="color: red;"> *</font>
				<input type="hidden" name="PageID" id="PageID" value="<?php echo $row['PageID'] ?>" />
			</td>
        </tr>
        <tr>
			<td align="right" width="120px">Title :&nbsp;&nbsp;</td>
			<td><input type="text" class="validate[required]" name="Title" id="Title" value="<?php echo $row['Title']; ?>" style="width: 120px;" /><font style="color: red;"> *</font></td>
		</tr>  	    
        <tr>
			<td align="right" width="120px">Link :&nbsp;&nbsp;</td>
			<td><input type="text" class="validate[required]" name="Link" id="Link" value="<?php echo $row['Link']; ?>" style="width: 120px;" /><font style="color: red;"> *</font></td>
		</tr> 
        <tr>
			<td align="right" width="120px">Picture :&nbsp;&nbsp;</td>
			<td><input type="file" name="Picture" id="Picture" value="<?php echo $row['Picture']; ?>" style="width: 120px;" /><font style="color: red;"> *</font></td>
		</tr>  	    
        <tr>
			<td align="right" width="120px">Description :&nbsp;&nbsp;</td>
			<td><textarea rows="7" name="Description" id="Description" cols="95"><?php echo $row['Description']; ?></textarea></td> /><font style="color: red;"> *</font></td>
		</tr>
        <tr>
			<td align="right" width="120px">FeaturedYN :&nbsp;&nbsp;</td>
			<td><input type="text" name="FeaturedYN" id="FeaturedYN" value="<?php echo $row['FeaturedYN']; ?>" style="width: 120px;" /><font style="color: red;"> *</font></td>
		</tr>
        <tr>
			<td align="right" width="120px">SortPriority :&nbsp;&nbsp;</td>
			<td><input type="text" name="SortPriority" id="SortPriority" value="<?php echo $row['SortPriority']; ?>" style="width: 120px;" /><font style="color: red;"> *</font></td>
		</tr>
        <tr style="font-weight:bold;">
			<td align="right">ActiveYN :&nbsp;&nbsp;</td>
			<td>
				<select size="1" name="ActiveYN">
        			 <?php
                if($row['ActiveYN'] == 'Yes')
                {
            ?>
                <option value="Yes">Yes</option>
                <option value="No">No</option>
            <?php
                }
                else
                {
            ?>
                <option value="No">No</option>
                <option value="Yes">Yes</option>
            <?php
                }
            ?>
			 </select>
			</td>
		</tr>
		<tr>
			<td></td>
			<td colspan="3" height="40">
				<input type="submit" name="btnUpdate" id="btnUpdate" value="Update" />
				<input type="reset" value="Reset" />
			</td>
		</tr>
    </table>
</form><br><br>
<i>Please fill in these fields (<font style="color: red;">*</font>)</i>
<?php
    }
   else
	{
		echo "<meta http-equiv='refresh' content='0; url=login.php' />";
	}
?>